package aos.ydksxt.service.jcxxcj;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjTdjcxx;
import aos.ydksxt.dao.DjTdjcxxTddjmx;
import aos.ydksxt.service.common.CommonService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 土地基础信息采集服务
 * Created by Stiofan on 2016/12/16.
 */
@Service
public class TdjcxxcjService extends CommonService {
    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setViewPath("/ydksxt/jcxxcj/tdjcxxcj.jsp");
    }

    @Transactional
    public void queryTdjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("tdzdh", inDto.get("tdzdh"));
        param.put("tdbh", inDto.get("tdbh"));
        DjTdjcxx djTdjcxx = (DjTdjcxx) persistenceDAO.findSingleByHql("from DjTdjcxx xx where xx.yxbz='Y' and xx.tdzdh=:tdzdh or xx.tdbh=:tdbh", param);
        Dto outDto = Dtos.newOutDto();
        if (ObjectUtils.anyNotNull(djTdjcxx)) {
            if (!httpModel.getUserModel().getAccount().equals(djTdjcxx.getSsglyDm()) && djTdjcxx.getSsglyDm() != null) {//判断税收管理员代码与当前操作人员不一致，给我提示
                checkSsgly(outDto, djTdjcxx.getSsglyDm());//校验税收管理员
            }
        } else {
            outDto.setAppCode("noTdjcxx");
        }
        outDto.putAll((Map) AOSUtils.object2Map(djTdjcxx));
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }

    @Transactional
    public void queryTdjcxxByTdbh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("tdbh", inDto.get("tdbh"));
        DjTdjcxx djTdjcxx = (DjTdjcxx) persistenceDAO.findSingleByHql("from DjTdjcxx xx where xx.yxbz='Y' and xx.tdbh=:tdbh", param);
        Dto outDto = Dtos.newOutDto();
        if (djTdjcxx != null) {
            outDto.setAppCode("suc");
            outDto.putAll((Map) AOSUtils.object2Map(djTdjcxx));
            outDto.put("zldz", getFullAddress(djTdjcxx.getXzqhdsDm(), djTdjcxx.getXzqhxqDm(), djTdjcxx.getJdxzDm(), djTdjcxx.getZldzSqjqDm(), djTdjcxx.getZldzDlxdDm(), null));
        } else {
            outDto.setAppCode("err");
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }

    @Transactional
    public void queryTdjcmxxxByTdbh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("tdbh", inDto.get("tdbh"));
        List results = persistenceDAO.findAllByHql("from DjTdjcxxTddjmx mx where mx.tdbh=:tdbh and mx.yxbz='Y'", param);
        httpModel.setOutMsg(AOSJson.toGridJson(results));
    }

    @Transactional
    public void querySebzByXzqhxzDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("xzqhszDm", inDto.get("xzqhszDm"));
        param.put("tddjDm", inDto.get("tddjDm"));
        List results = persistenceDAO.findAllBySql("select b.tddj_dm as value, b.dwse as display\n" +
                "  from hx_cs_zdy.cs_dj_tddwseszb b\n" +
                " where b.xybz = 'Y'\n" +
                "   and b.yxbz = 'Y'\n" +
                "   and b.xzqhsz_dm = :xzqhszDm\n" +
                "   and b.tddj_dm = :tddjDm", param);
        httpModel.setOutMsg(AOSJson.toJson(results));
    }

    @Transactional
    public void saveTdjcmxxxByTdbh(HttpModel httpModel) {
        Dto outDto = Dtos.newOutDto();
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("tdbh", inDto.get("tdbh"));
        param.put("tddjmxuuid", inDto.get("tddjmxuuid"));
        DjTdjcxxTddjmx djTdjcxxTddjmx = (DjTdjcxxTddjmx) persistenceDAO.findSingleByHql("from DjTdjcxxTddjmx mx where mx.tdbh=:tdbh and mx.tddjmxuuid=:tddjmxuuid and mx.yxbz='Y'", param);
        if (djTdjcxxTddjmx == null) {
            djTdjcxxTddjmx = new DjTdjcxxTddjmx();
        }
        AOSUtils.copyProperties(inDto, djTdjcxxTddjmx);
        djTdjcxxTddjmx.setYxbz("Y");
        if (StringUtils.isEmpty(djTdjcxxTddjmx.getTddjmxuuid())) {
            djTdjcxxTddjmx.setTddjmxuuid(getRandomUUID());
            djTdjcxxTddjmx.setLrrDm(getCzryDm(httpModel));
            djTdjcxxTddjmx.setLrrq(getDateTime());
        } else {
            djTdjcxxTddjmx.setXgrDm(getCzryDm(httpModel));
            djTdjcxxTddjmx.setXgrq(getDateTime());
        }

        persistenceDAO.saveOrUpdate(djTdjcxxTddjmx);
        param.clear();
        param.put("tdbh", djTdjcxxTddjmx.getTdbh());
        List djTdjcxxMxs = persistenceDAO.findAllByHql("from DjTdjcxxTddjmx mx where mx.tdbh=:tdbh and mx.yxbz='Y'", param);
        DjTdjcxx djTdjcxx = (DjTdjcxx) persistenceDAO.findSingleByHql("from DjTdjcxx xx where xx.tdbh=:tdbh and xx.yxbz='Y'", param);
        djTdjcxx.setTdzmj(new BigDecimal(0));
        djTdjcxx.setTdzyz(new BigDecimal(0));
        for (Object object : djTdjcxxMxs) {
            DjTdjcxxTddjmx mx = (DjTdjcxxTddjmx) object;
            if(djTdjcxx.getTdzmj()==null){
                djTdjcxx.setTdzmj(new BigDecimal(0));
            }
            if (djTdjcxx.getTdzyz()==null){
                djTdjcxx.setTdzyz(new BigDecimal(0));
            }
            if (djTdjcxx.getTdzynse()==null){
                djTdjcxx.setTdzynse(new BigDecimal(0));
            }
            djTdjcxx.setTdzmj(djTdjcxx.getTdzmj().add(mx.getTdmj()).setScale(2, BigDecimal.ROUND_UNNECESSARY));
            djTdjcxx.setTdzyz(djTdjcxx.getTdzyz().add(mx.getTdyz()).setScale(2, BigDecimal.ROUND_UNNECESSARY));
            djTdjcxx.setTdzynse(djTdjcxx.getTdzynse().add(mx.getYnse().setScale(2, BigDecimal.ROUND_UNNECESSARY)));
        }
        outDto.setAppCode("suc");
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }

    @Transactional
    public void saveTdjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("tdbh", inDto.getString("tdbh"));
        DjTdjcxx djTdjcxx = (DjTdjcxx) persistenceDAO.findSingleByHql("from DjTdjcxx xx where xx.tdbh=:tdbh", param);
        if (djTdjcxx == null) {
            djTdjcxx = new DjTdjcxx();
        }
        AOSUtils.copyProperties(inDto, djTdjcxx);
        djTdjcxx.setYxbz("Y");
        BigDecimal TdyzValue = new BigDecimal("0");
        if (djTdjcxx.getTdzyz() == null) {
            djTdjcxx.setTdzyz(TdyzValue);
        }
        djTdjcxx.setSsglyDm(getCzryDm(httpModel));
        djTdjcxx.setSjgsqs(getCzjgDm(httpModel));
        if (StringUtils.isEmpty(djTdjcxx.getTdbh())) {
            djTdjcxx.setLrrDm(getCzryDm(httpModel));
            djTdjcxx.setLrrq(getDateTime());
            djTdjcxx.setTdbh(getSybh(djTdjcxx.getXzqhxqDm()));
        } else {
            djTdjcxx.setXgrDm(getCzryDm(httpModel));
            djTdjcxx.setXgrq(getDateTime());
        }
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djTdjcxx);
            outDto.setAppCode("suc");
            outDto.putAll((Map) AOSUtils.object2Map(djTdjcxx));
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }

    @Transactional
    public void zfTdjcxxByTdbh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("tdbh", inDto.get("tdbh"));
        List tdjcxxmxs = persistenceDAO.findAllByHql("from DjTdjcxxTddjmx mx where mx.tdbh=:tdbh and mx.yxbz='Y'", param);
        Dto outDto = Dtos.newOutDto();
        for (Object object : tdjcxxmxs) {
            DjTdjcxxTddjmx djTdjcxxMx = (DjTdjcxxTddjmx) object;
            djTdjcxxMx.setXgrDm(getCzryDm(httpModel));
            djTdjcxxMx.setXgrq(getDateTime());
            djTdjcxxMx.setZfrDm(getCzryDm(httpModel));
            djTdjcxxMx.setZfrq(getDateTime());
            djTdjcxxMx.setYxbz("N");
            djTdjcxxMx.setZfyy(inDto.getString("zfyy"));
            try {
                persistenceDAO.saveOrUpdate(djTdjcxxMx);
                outDto.setAppCode("suc");
            } catch (Exception e) {
                outDto.setAppCode("err");
                outDto.setAppMsg(e.getMessage());
                httpModel.setOutMsg(AOSJson.toJson(outDto));
                return;
            }
        }
        DjTdjcxx djTdjcxx = (DjTdjcxx) persistenceDAO.findSingleByHql("from DjTdjcxx xx where xx.tdbh=:tdbh", param);
        djTdjcxx.setXgrDm(getCzryDm(httpModel));
        djTdjcxx.setXgrq(getDateTime());
        djTdjcxx.setZfrDm(getCzryDm(httpModel));
        djTdjcxx.setZfrq(getDateTime());
        djTdjcxx.setZfyy(inDto.getString("zfyy"));
        djTdjcxx.setYxbz("N");
        try {
            persistenceDAO.saveOrUpdate(djTdjcxx);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }

    @Transactional
    public void zfTdjcxxMxByTdsymxuuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("tddjmxuuid", inDto.get("tddjmxuuid"));
        DjTdjcxxTddjmx djTdjcxxMx = (DjTdjcxxTddjmx) persistenceDAO.findSingleByHql("from DjTdjcxxTddjmx mx where mx.tddjmxuuid=:tddjmxuuid", param);
        djTdjcxxMx.setXgrDm(getCzryDm(httpModel));
        djTdjcxxMx.setXgrq(getDateTime());
        djTdjcxxMx.setZfrDm(getCzryDm(httpModel));
        djTdjcxxMx.setZfrq(getDateTime());
        djTdjcxxMx.setZfyy(inDto.getString("zfyyMx"));
        djTdjcxxMx.setYxbz("N");
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djTdjcxxMx);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }

    @Transactional
    public void queryTddjDmByXzqhxzDm(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto();
        String querySql = "select distinct dj.tddj_dm as value,\n" +
                "                (case\n" +
                "                  when dj.sjtddj_dm = '100' then\n" +
                "                   '城市-' || dj.tddjmc\n" +
                "                  when dj.sjtddj_dm = '200' then\n" +
                "                   '县城-' || dj.tddjmc\n" +
                "                  when dj.sjtddj_dm = '300' then\n" +
                "                   '建制镇-' || dj.tddjmc\n" +
                "                  when dj.sjtddj_dm = '400' then\n" +
                "                   '工矿区-' || dj.tddjmc\n" +
                "                end) as display\n" +
                "  from hx_dm_qg.dm_dj_tddj dj, hx_cs_zdy.cs_dj_tddwseszb zb\n" +
                " where dj.yxbz = 'Y'\n" +
                "   and dj.xybz = 'Y'\n" +
                "   and dj.tddj_dm = zb.tddj_dm\n";
        if (!StringUtils.isEmpty(inDto.getString("xzqhszDm"))) {
            querySql += "   and zb.xzqhsz_dm = :xzqhszDm\n";
            params.put("xzqhszDm", inDto.get("xzqhszDm"));
        }
        querySql += " order by dj.tddj_dm";
        List tddjDms = persistenceDAO.findAllBySql(querySql, params);
        httpModel.setOutMsg(AOSJson.toJson(AOSUtils.object2Map(tddjDms)));
    }

}
